package sizeyunsuan;

import java.util.ArrayList;
import java.util.Stack;
import java.util.StringTokenizer;

/**
 * Created by 春旺 on 2017/5/17.
 */
public class Calculate2 {
    public ArrayList<String> getStringList(String str) {
    ArrayList<String> result = new ArrayList<String>();

    StringTokenizer tokenizer = new StringTokenizer(str," ");
    while(tokenizer.hasMoreTokens()) {
          result.add(tokenizer.nextToken());

        }
    return result;
}


    public ArrayList<String> getPostOrder(ArrayList<String> inOrderList) {

        ArrayList<String> result = new ArrayList<String>();
        Stack<String> stack = new Stack<String>();
        for (int i = 0; i < inOrderList.size(); i++) {
            if (Character.isDigit(inOrderList.get(i).charAt(0))) {
                result.add(inOrderList.get(i));
            } else {
               switch (inOrderList.get(i).charAt(0)) {
                    default:
                        while (!stack.isEmpty() && compare(stack.peek(), inOrderList.get(i))) {
                            result.add(stack.pop());
                        }
                        stack.push(inOrderList.get(i));
                        break;
                }
            }
        }
        while (!stack.isEmpty()) {
            result.add(stack.pop());
        }
        return result;
    }


    public Integer calculate(ArrayList<String> postOrder) {
        Stack stack = new Stack();
        for (int i = 0; i < postOrder.size(); i++) {
            if (Character.isDigit(postOrder.get(i).charAt(0))) {
                stack.push(Integer.parseInt(postOrder.get(i)));
            } else {
                Integer back = (Integer) stack.pop();

                Integer front = (Integer) stack.pop();
                Integer res = 0;
                switch (postOrder.get(i).charAt(0)) {
                    case '+':
                        res = front + back;
                        break;
                    case '-':
                        res = front - back;
                        break;
                    case '*':
                        res = front * back;
                        break;
                    case '/':
                        RationalNumber rationalNumber = new RationalNumber(front, back);
                        RationalNumber a = rationalNumber;

                        // res = front / back;
                        break;
                }
                stack.push(res);
            }
        }
        return (Integer) stack.pop();
    }


    public static boolean compare(String peek, String cur) {
        if ("*".equals(peek) && ("/".equals(cur) || "*".equals(cur) || "+".equals(cur) || "-".equals(cur))) {
            return true;
        } else if ("/".equals(peek) && ("/".equals(cur) || "*".equals(cur) || "+".equals(cur) || "-".equals(cur))) {
            return true;
        } else if ("+".equals(peek) && ("+".equals(cur) || "-".equals(cur))) {
            return true;
        } else if ("-".equals(peek) && ("+".equals(cur) || "-".equals(cur))) {
            return true;
        }
        return false;
    }

    public int calc(String a) {
        Calculate2 calculate = new Calculate2();
        String s = a;
        ArrayList result = calculate.getStringList(s);  //String转换为List
        result = calculate.getPostOrder(result);   //中缀变后缀
        int i = calculate.calculate(result);   //计算
        return i;
    }

}
